package cn.iocoder.yudao.module.game.service.mail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.mail.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.mail.MailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.mail.MailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 邮件 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MailServiceImpl implements MailService {

    @Resource
    private MailMapper mailMapper;

    @Override
    public Long createMail(MailSaveReqVO createReqVO) {
        // 插入
        MailDO mail = BeanUtils.toBean(createReqVO, MailDO.class);
        mailMapper.insert(mail);
        // 返回
        return mail.getId();
    }

    @Override
    public void updateMail(MailSaveReqVO updateReqVO) {
        // 校验存在
        validateMailExists(updateReqVO.getId());
        // 更新
        MailDO updateObj = BeanUtils.toBean(updateReqVO, MailDO.class);
        mailMapper.updateById(updateObj);
    }

    @Override
    public void deleteMail(Long id) {
        // 校验存在
        validateMailExists(id);
        // 删除
        mailMapper.deleteById(id);
    }

    private void validateMailExists(Long id) {
        if (mailMapper.selectById(id) == null) {
            throw exception(MAIL_NOT_EXISTS);
        }
    }

    @Override
    public MailDO getMail(Long id) {
        return mailMapper.selectById(id);
    }

    @Override
    public PageResult<MailDO> getMailPage(MailPageReqVO pageReqVO) {
        return mailMapper.selectPage(pageReqVO);
    }

}